package cc.admin.modules.sys.controller;

import cc.admin.common.api.vo.Result;
import cc.admin.common.aspect.annotation.AutoLog;
import cc.admin.common.sys.base.controller.BaseController;
import cc.admin.common.sys.query.QueryGenerator;
import cc.admin.modules.sys.entity.SysModule;
import cc.admin.modules.sys.service.ISysModuleService;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 模块管理
 * @Author: cc-admin
 * @Date: 2021-12-30
 * @Version: V1.0.0
 */
@Slf4j
@Api(tags = "模块管理")
@RestController
@RequestMapping("/sys/module")
public class SysModuleController extends BaseController<SysModule, ISysModuleService> {
    @Autowired
    private ISysModuleService sysModuleService;

    /**
     * 分页列表查询
     *
     * @param key
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "模块管理-分页列表查询")
    @ApiOperation(value = "模块管理-分页列表查询", notes = "模块管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(
            @RequestParam(name = "key", required = false) String key,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        QueryWrapper<SysModule> queryWrapper = QueryGenerator.initQueryWrapper(new SysModule(), req.getParameterMap());
        if (StrUtil.isNotEmpty(key)) {
            queryWrapper.and(i -> i.like("module_name", key));
        }
        queryWrapper.orderByAsc("module_code");
        Page<SysModule> page = new Page<SysModule>(pageNo, pageSize);
        IPage<SysModule> pageList = sysModuleService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param sysModule
     * @return
     */
    @AutoLog(value = "模块管理-添加")
    @ApiOperation(value = "模块管理-添加", notes = "模块管理-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody SysModule sysModule) {
        sysModuleService.save(sysModule);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param sysModule
     * @return
     */
    @AutoLog(value = "模块管理-编辑")
    @ApiOperation(value = "模块管理-编辑", notes = "模块管理-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody SysModule sysModule) {
        sysModuleService.updateById(sysModule);
        return Result.ok("编辑成功!");
    }


    /**
     * 复制
     *
     * @param id
     * @param name
     * @return
     */
    @AutoLog(value = "模块管理-复制")
    @ApiOperation(value = "模块管理-复制", notes = "模块管理-复制")
    @PostMapping(value = "/copy")
    public Result<?> copy(@RequestParam(name = "id", required = true) String id, @RequestParam(name = "name", required = true) String name) {
        SysModule sysModule = sysModuleService.getById(id);
        sysModule.setId(IdUtil.fastUUID());
       // sysModule.setName(name);
        sysModuleService.save(sysModule);
        return Result.ok("复制成功！");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "模块管理-通过id删除")
    @ApiOperation(value = "模块管理-通过id删除", notes = "模块管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        sysModuleService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "模块管理-批量删除")
    @ApiOperation(value = "模块管理-批量删除", notes = "模块管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.sysModuleService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "模块管理-通过id查询")
    @ApiOperation(value = "模块管理-通过id查询", notes = "模块管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        SysModule sysModule = sysModuleService.getById(id);
        return Result.ok(sysModule);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param sysModule
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysModule sysModule) {
        return super.exportXls(request, sysModule, SysModule.class, "模块管理");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SysModule.class);
    }

    /**
     * 模块下拉
     * @return
     */
    @RequestMapping(value = "/getModule", method = RequestMethod.GET)
    public Result<?> getModule(){
        List<Map<String,Object>> dataList = new ArrayList<>();
        sysModuleService.list().forEach(item->{
            Map<String,Object> map = new HashMap<>();
            map.put("label", item.getModuleName());
            map.put("value", item.getModuleCode());
            dataList.add(map);
        });
        return Result.ok(dataList);
    }

}
